package main

import (
	"blockchain_example/core"
	"fmt"
	"math/rand"
	"time"
)

func main() {
	myAccount := core.NewAccount()
	//初始化一个区块链实例
	myBlockchain := core.NewBlockchain(2)
	go func() {
		for i := 1; i <= 6; i++ {
			myBlockchain.CreateNewBlock()

		}
	}()
	myAccount.CompleteTransaction("nil null 0")
	//向区块链中先后添加6笔交易，每笔交易间延时1~3秒，模拟交易间的先后顺序
	myBlockchain.SendTransaction("Tom Mary 20")
	myAccount.CompleteTransaction("Tom Mary 20")
	time.Sleep(time.Duration(rand.Intn(3)+1) * time.Second)
	myBlockchain.SendTransaction("Tom Lily 30")
	myAccount.CompleteTransaction("Tom Lily 30")
	time.Sleep(time.Duration(rand.Intn(3)+1) * time.Second)
	myBlockchain.SendTransaction("Aron Tom 10")
	myAccount.CompleteTransaction("Aron Tom 10")
	time.Sleep(time.Duration(rand.Intn(3)+1) * time.Second)
	myBlockchain.SendTransaction("Byron Lily 15")
	myAccount.CompleteTransaction("Byron Lily 15")
	time.Sleep(time.Duration(rand.Intn(20)+1) * time.Second)
	myBlockchain.SendTransaction("Joe Tom 20")
	myAccount.CompleteTransaction("Joe Tom 20")
	time.Sleep(time.Duration(rand.Intn(20)+1) * time.Second)
	myBlockchain.SendTransaction("Lily Mary 40")
	myAccount.CompleteTransaction("Lily Mary 40")

	//休息一下~
	time.Sleep(20 * time.Second)
	//打印区块链中的交易历史
	myBlockchain.Print()
	fmt.Printf("\n")
	myAccount.PrintList()
}
